
clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user

set graphics on  

global path_data_processed "\data\processed"
global path_output "\data\output"

clear all
set more off


*** Panel A

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen posterior_higheduc=posterior*atleast_bachelor
gen treatment_higheduc=treatment*atleast_bachelor
gen posterior_loweduc=posterior*(1-atleast_bachelor)
gen treatment_loweduc=treatment*(1-atleast_bachelor)

ivreg2 jobinsecurity (posterior posterior_higheduc=treatment treatment_higheduc) $controls , r
replace coeff=_b[posterior] if atleast_bachelor==0
replace standarderror=_se[posterior] if atleast_bachelor==0

ivreg2 jobinsecurity (posterior posterior_loweduc=treatment treatment_loweduc) $controls, r
replace coeff=_b[posterior] if  atleast_bachelor==1
replace standarderror=_se[posterior] if  atleast_bachelor==1

collapse (mean) coeff standarderror, by(atleast_bachelor)

gen id_2=1 if atleast_bachelor==0
replace id_2=2.5 if  atleast_bachelor==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0
cd "$path_tables"

twoway  (rbar coeff zero id_2 if atleast_bachelor==0) (rbar coeff zero id_2 if atleast_bachelor==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Personal unemployment") ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.1(0.05).3, nogrid) ///
xlabel( 1 "Below college" 2.5 "College", noticks angle(0))
graph save "jobsecurity_educ_interact.gph", replace

***

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen posterior_highearn=posterior*highearn
gen treatment_highearn=treatment*highearn
gen posterior_lowearn=posterior*(1-highearn)
gen treatment_lowearn=treatment*(1-highearn)

ivreg2 jobinsecurity (posterior posterior_highearn=treatment treatment_highearn) highearn $controls , r
replace coeff=_b[posterior] if highearn==0
replace standarderror=_se[posterior] if highearn==0

ivreg2 jobinsecurity (posterior posterior_lowearn=treatment treatment_lowearn) highearn $controls, r
replace coeff=_b[posterior] if  highearn==1
replace standarderror=_se[posterior] if  highearn==1

collapse (mean) coeff standarderror, by(highearn)

gen id_2=1 if  highearn==0
replace id_2=2.5 if  highearn==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if highearn==0) (rbar coeff zero id_2 if highearn==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Personal unemployment") ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.1(0.05).3, nogrid) ///
xlabel( 1 "High earn" 2.5 "Low earn", noticks angle(0))
graph save "jobsecurity_earn_interact.gph", replace

***

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen posterior_old=posterior*old
gen treatment_old=treatment*old
gen posterior_young=posterior*(1-old)
gen treatment_young=treatment*(1-old)

ivreg2 jobinsecurity (posterior posterior_old=treatment treatment_old) old $controls , r
replace coeff=_b[posterior] if old==0
replace standarderror=_se[posterior] if old==0

ivreg2 jobinsecurity (posterior posterior_young=treatment treatment_young) old $controls, r
replace coeff=_b[posterior] if  old==1
replace standarderror=_se[posterior] if  old==1

collapse (mean) coeff standarderror, by(old)

gen id_2=1 if  old==0
replace id_2=2.5 if  old==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if old==0) (rbar coeff zero id_2 if old==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Personal unemployment") ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.1(0.05).3, nogrid) ///
xlabel( 1 "Age<45" 2.5 "Age>44", noticks angle(0))
graph save "jobsecurity_age_interact.gph", replace

***

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen posterior_female=posterior*female
gen treatment_female=treatment*female
gen posterior_male=posterior*(1-female)
gen treatment_male=treatment*(1-female)

ivreg2 jobinsecurity (posterior posterior_female=treatment treatment_female) $controls , r
replace coeff=_b[posterior] if female==0
replace standarderror=_se[posterior] if female==0

ivreg2 jobinsecurity (posterior posterior_male=treatment treatment_male) $controls, r
replace coeff=_b[posterior] if  female==1
replace standarderror=_se[posterior] if  female==1

collapse (mean) coeff standarderror, by(female)

gen id_2=1 if  female==0
replace id_2=2.5 if  female==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if female==0) (rbar coeff zero id_2 if female==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Personal unemployment") ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.1(0.05).3, nogrid) ///
xlabel( 1 "Male" 2.5 "Female", noticks angle(0))
graph save "jobsecurity_gender_interact.gph", replace

	   
cd "$path_output"
graph combine "jobsecurity_educ_interact.gph" "jobsecurity_earn_interact.gph" "jobsecurity_age_interact.gph" "jobsecurity_gender_interact.gph", graphregion(fcolor(white)) ycommon xcommon ysize(8) xsize(16) col(4) title("Panel A" , size(large)) iscale(*.8)
graph save "jobsecurity_demographics_interact.gph", replace


*** Panel B

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen posterior_higheduc=posterior*atleast_bachelor
gen treatment_higheduc=treatment*atleast_bachelor
gen posterior_loweduc=posterior*(1-atleast_bachelor)
gen treatment_loweduc=treatment*(1-atleast_bachelor)

ivreg2 earn_mean (posterior posterior_higheduc=treatment treatment_higheduc) $controls , r
replace coeff=_b[posterior] if atleast_bachelor==0
replace standarderror=_se[posterior] if atleast_bachelor==0

ivreg2 earn_mean (posterior posterior_loweduc=treatment treatment_loweduc) $controls, r
replace coeff=_b[posterior] if  atleast_bachelor==1
replace standarderror=_se[posterior] if  atleast_bachelor==1

collapse (mean) coeff standarderror, by(atleast_bachelor)

gen id_2=1 if atleast_bachelor==0
replace id_2=2.5 if  atleast_bachelor==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if atleast_bachelor==0) (rbar coeff zero id_2 if atleast_bachelor==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Earnings growth: Mean") ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.1(0.02).1, nogrid) ///
xlabel( 1 "Below college" 2.5 "College", noticks angle(0))
graph save "earnings_educ_interact.gph", replace


***

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
	
foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen posterior_highearn=posterior*highearn
gen treatment_highearn=treatment*highearn
gen posterior_lowearn=posterior*(1-highearn)
gen treatment_lowearn=treatment*(1-highearn)

ivreg2 earn_mean (posterior posterior_highearn=treatment treatment_highearn) highearn $controls , r
replace coeff=_b[posterior] if highearn==0
replace standarderror=_se[posterior] if highearn==0

ivreg2 earn_mean (posterior posterior_lowearn=treatment treatment_lowearn) highearn $controls, r
replace coeff=_b[posterior] if  highearn==1
replace standarderror=_se[posterior] if  highearn==1

collapse (mean) coeff standarderror, by(highearn)

gen id_2=1 if  highearn==0
replace id_2=2.5 if  highearn==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if highearn==0) (rbar coeff zero id_2 if highearn==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Earnings growth: Mean") ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.1(0.02).1, nogrid) ///
xlabel( 1 "High earn" 2.5 "Low earn", noticks angle(0))
graph save "earnings_earn_interact.gph", replace

***

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
	
foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen posterior_old=posterior*old
gen treatment_old=treatment*old
gen posterior_young=posterior*(1-old)
gen treatment_young=treatment*(1-old)

ivreg2 earn_mean (posterior posterior_old=treatment treatment_old) old $controls , r
replace coeff=_b[posterior] if old==0
replace standarderror=_se[posterior] if old==0

ivreg2 earn_mean (posterior posterior_young=treatment treatment_young) old $controls, r
replace coeff=_b[posterior] if  old==1
replace standarderror=_se[posterior] if  old==1

collapse (mean) coeff standarderror, by(old)

gen id_2=1 if  old==0
replace id_2=2.5 if  old==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if old==0) (rbar coeff zero id_2 if old==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Earnings growth: Mean") ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.1(0.02).1, nogrid) ///
xlabel( 1 "Age<45" 2.5 "Age>44", noticks angle(0))
graph save "earnings_age_interact.gph", replace

***

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
	
foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen posterior_female=posterior*female
gen treatment_female=treatment*female
gen posterior_male=posterior*(1-female)
gen treatment_male=treatment*(1-female)

ivreg2 earn_mean (posterior posterior_female=treatment treatment_female) $controls , r
replace coeff=_b[posterior] if female==0
replace standarderror=_se[posterior] if female==0

ivreg2 earn_mean (posterior posterior_male=treatment treatment_male) $controls, r
replace coeff=_b[posterior] if  female==1
replace standarderror=_se[posterior] if  female==1

collapse (mean) coeff standarderror, by(female)

gen id_2=1 if  female==0
replace id_2=2.5 if  female==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if female==0) (rbar coeff zero id_2 if female==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Earnings growth: Mean") ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.1(0.02).1, nogrid) ///
xlabel( 1 "Male" 2.5 "Female", noticks angle(0))
graph save "earnings_gender_interact.gph", replace


*** Combining figures on demographics

cd "$path_output"

graph combine "earnings_educ_interact.gph" "earnings_earn_interact.gph" "earnings_age_interact.gph" "earnings_gender_interact.gph", graphregion(fcolor(white)) ycommon xcommon col(4) ysize(8) xsize(16) title("Panel B" , size(large)) iscale(*.8)
graph save "earnings_demographics_interact.gph",  replace



*** Panel C

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen ind_cyclical=1-health_educ-ind_other
tab ind_cyclical, m
gen ind_noncyclical=1-ind_cyclical

gen posterior_ind_noncyclical=posterior*ind_noncyclical
gen treatment_ind_noncyclical=treatment*ind_noncyclical
gen posterior_ind_cyclical=posterior*(1-ind_noncyclical)
gen treatment_ind_cyclical=treatment*(1-ind_noncyclical)

ivreg2 jobinsecurity (posterior posterior_ind_noncyclical=treatment treatment_ind_noncyclical) ind_noncyclical $controls , r
replace coeff=_b[posterior] if ind_noncyclical==0
replace standarderror=_se[posterior] if ind_noncyclical==0

ivreg2 jobinsecurity (posterior posterior_ind_cyclical=treatment treatment_ind_cyclical) ind_noncyclical $controls, r
replace coeff=_b[posterior] if  ind_noncyclical==1
replace standarderror=_se[posterior] if  ind_noncyclical==1

collapse (mean) coeff standarderror, by(ind_noncyclical)

gen id_2=1 if  ind_noncyclical==0
replace id_2=2.5 if  ind_noncyclical==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if ind_noncyclical==0) (rbar coeff zero id_2 if ind_noncyclical==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Personal unemployment" /*, size(*1.5)*/) ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.2(0.1).6, /*labsize(*1.5)*/ nogrid) ///
xlabel( 1 "Cyclical industry" 2.5 "Noncyclical industry", noticks angle(0) /*labsize(*1.5)*/)
graph save "jobsecurity_industry_interact.gph", replace


***

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen posterior_never_unemp=posterior*never_unemployed_imp
gen treatment_never_unemp=treatment*never_unemployed_imp
gen posterior_ever_unemp=posterior*(1-never_unemployed_imp)
gen treatment_ever_unemp=treatment*(1-never_unemployed_imp)

ivreg2 jobinsecurity (posterior posterior_never_unemp=treatment treatment_never_unemp) never_unemployed_imp $controls , r
replace coeff=_b[posterior] if never_unemployed_imp==0
replace standarderror=_se[posterior] if never_unemployed_imp==0

ivreg2 jobinsecurity (posterior posterior_ever_unemp=treatment treatment_ever_unemp) never_unemployed_imp $controls, r
replace coeff=_b[posterior] if  never_unemployed_imp==1
replace standarderror=_se[posterior] if  never_unemployed_imp==1

collapse (mean) coeff standarderror, by(never_unemployed_imp)

gen id_2=1 if  never_unemployed_imp==0
replace id_2=2.5 if  never_unemployed_imp==1
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if never_unemployed_imp==0) (rbar coeff zero id_2 if never_unemployed_imp==1) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Personal unemployment" /*, size(*1.5)*/) ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.2(0.1).6, /*labsize(*1.5)*/ nogrid) ///
xlabel(1 "Ever unemployed" 2.5 "Never unemployed", noticks angle(0) /*labsize(*1.5)*/)
graph save "jobsecurity_unempexper_interact.gph", replace


***

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen coeff=.
gen standarderror=.

gen high_county_unemp=highunemp_rate3y

gen posterior_high_unemp=posterior*high_county_unemp
gen treatment_high_unemp=treatment*high_county_unemp
gen posterior_low_unemp=posterior*(1-high_county_unemp)
gen treatment_low_unemp=treatment*(1-high_county_unemp)

ivreg2 jobinsecurity (posterior posterior_low_unemp=treatment treatment_low_unemp) high_county_unemp $controls , r
replace coeff=_b[posterior] if high_county_unemp==1
replace standarderror=_se[posterior] if high_county_unemp==1

ivreg2 jobinsecurity (posterior posterior_high_unemp=treatment treatment_high_unemp) high_county_unemp $controls, r
replace coeff=_b[posterior] if  high_county_unemp==0
replace standarderror=_se[posterior] if  high_county_unemp==0

collapse (mean) coeff standarderror, by(high_county_unemp)

gen id_2=1 if  high_county_unemp==1
replace id_2=2.5 if  high_county_unemp==0
 
generate hiz_avg = coeff + 1.645*(standarderror)
generate lowz_avg = coeff - 1.645*(standarderror)

gen zero = 0

twoway  (rbar coeff zero id_2 if high_county_unemp==1) (rbar coeff zero id_2 if high_county_unemp==0) (rcap hiz_avg lowz_avg id_2 ) , ///	
ytitle("Personal unemployment" /*, size(*1.5)*/) ///	
xtitle("Effect of recession exp.") ///
legend(off) ///
yline(0) ///
ylabel(-0.2(0.1).6, /*labsize(*1.5)*/ nogrid) ///
xlabel(1 "High county unempl." 2.5 "Low county unempl.", noticks angle(0) /*labsize(*1.5)*/)
graph save "jobsecurity_countyunemp_interact.gph", replace


cd "$path_output"

graph combine "jobsecurity_industry_interact.gph"  "jobsecurity_unempexper_interact.gph" "jobsecurity_countyunemp_interact.gph", title("Panel C"  , size(large)) iscale(*.8) ///
graphregion(fcolor(white)) ycommon xcommon cols(3) ysize(4) xsize(12) 
graph save "jobsecurity_riskproxies_interact.gph", replace


graph combine "jobsecurity_demographics_interact.gph" "earnings_demographics_interact.gph" "jobsecurity_riskproxies_interact.gph", ///
graphregion(fcolor(white)) col(1) ysize(12) xsize(10) iscale(*.815)
graph export "heterogeneitymain.pdf", as(pdf) replace








